﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Foundation;
using Business;
using System.IO;

namespace RPGSoundmixer {
    public partial class EffektControl : Form {
        private Effekt effekt;
        private CategorieCollection collection = CategorieCollection.GetInstance();
        private IDataHandler handler; 

        public EffektControl() {
            InitializeComponent();
        }

        public EffektControl(Effekt p){
            this.handler = DataHandler.GetInstance();
            
            this.InitializeComponent();
            this.effekt = p;
            this.txtName.Text = this.effekt.Name;
            this.txtKey.Text = this.effekt.Key.ToString();

            foreach (Track track in this.effekt.Tracks) {
                this.lstTracks.Items.Add(track);
            }

            this.cmbType.DataSource = Enum.GetValues(typeof(EffektTypeEnum));
            this.cmbType.SelectedIndex = (int)this.effekt.Type;
            this.trackBarVolume.Value = this.effekt.Volume;

            this.cmbCategories.Items.AddRange(handler.Categories.ToArray()); 

        }

        private void label2_Click(object sender, EventArgs e) {

        }

        private void EffektControl_Load(object sender, EventArgs e) {
           

        }

        private void button3_Click(object sender, EventArgs e) {
            this.effekt.Type = (EffektTypeEnum)this.cmbType.SelectedIndex;
            this.effekt.Volume = this.trackBarVolume.Value; 
            this.Close();
        }

        private void txtName_TextChanged(object sender, EventArgs e) {
            this.effekt.Name = this.txtName.Text;
        }
        

        private void txtKey_TextChanged(object sender, EventArgs e) {
            if (!string.IsNullOrEmpty(this.txtKey.Text)) {
                this.effekt.Key = this.txtKey.Text.ToCharArray()[0];
            }
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e) {
                        
        }

        private void refreshEffektTrakList() {
            this.lstTracks.Items.Clear();
            foreach (Track s in this.effekt.Tracks) {
                this.lstTracks.Items.Add(s);                
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK) {

                foreach (var file in openFileDialog1.FileNames) {
                    effekt.Tracks.Add(new Track(Path.GetFileName(file), Path.GetDirectoryName(file)));
                }
                refreshEffektTrakList();
            }
        }

        private void btTrackRemove_Click(object sender, EventArgs e) {
            if(lstTracks.SelectedItem != null){
                this.effekt.Tracks.Remove((Track)lstTracks.SelectedItem);
                this.refreshEffektTrakList();
            }
        }

        private void label4_Click(object sender, EventArgs e) {

        }

        private void cmbCategories_SelectedIndexChanged(object sender, EventArgs e) {
            ComboBox lbox = (ComboBox)sender;
            Category c  = (Category)lbox.SelectedItem;
            this.listTaqggedItems.Items.Clear(); 
            this.listTaqggedItems.Items.AddRange(handler.TracksInCategory(c).ToArray());
        }

        private void button2_Click(object sender, EventArgs e) {
            this.effekt.Tracks.Add((Track)this.listTaqggedItems.SelectedItem);
            refreshEffektTrakList();
        }

             
    }
}
